#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped)
    {
        vector<int> stack;
        int j = 0;
        for (int i = 0; i < pushed.size(); i++)
        {
            stack.push_back(pushed[i]);
            while (j < popped.size() && !stack.empty() && popped[j] == stack.back())
            {
                stack.pop_back();
                j++;
            }
        }
        return stack.empty();
    }
};